package response

import (
	"MyBlogv2/blog-common/HttpCode"
	"github.com/gin-gonic/gin"
	"net/http"
)

func ResponseOK(c *gin.Context, data interface{}) {
	response := gin.H{
		"code": HttpCode.SUCCESS,
		"msg":  HttpCode.AppHttpCodeEnumMsg[HttpCode.SUCCESS],
	}
	if data != nil {
		response["data"] = data
	}
	c.JSON(http.StatusOK, response)
}

func ResponseError(c *gin.Context, code HttpCode.AppHttpCodeEnum) {
	response := gin.H{
		"code": code,
		"msg":  HttpCode.AppHttpCodeEnumMsg[code],
	}
	c.JSON(200, response)
}
func ResponseErrorWithMsg(c *gin.Context, code HttpCode.AppHttpCodeEnum, msg string) {
	response := gin.H{
		"code": code,
		"msg":  msg,
	}
	c.JSON(0, response)
}
func CommonResponse(c *gin.Context, code HttpCode.AppHttpCodeEnum, data interface{}, msg string) {
	response := gin.H{
		"code": code,
	}
	if data != nil {
		response["data"] = data
	}
	if msg != "" {
		response["msg"] = msg
	} else {
		response["msg"] = HttpCode.AppHttpCodeEnumMsg[code]
	}
	if code == HttpCode.SUCCESS {
		c.JSON(http.StatusOK, response)
	} else {
		c.JSON(0, response)
	}
}
